package fr.umlv.android.caveAvin.sqlite;

import java.util.ArrayList;

import fr.umlv.android.caveAVin.beans.Bottle;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class BottleBDD extends MyProjectBDD {

	private static final long serialVersionUID = 1L;
	
	private static final String TABLE_BOTTLE= "table_bottle";
	
	private static final String COL_ID_BOTTLE = "Id_bottle";
	
	private static final int NUM_COL_ID = 0;
	
	private static final String COL_DESIGNATION = "designation";
	
	private static final int NUM_COL_DESIGNATION  = 1;
	
	private static final String COL_PRODUCERNAME = "producer_name";
	
	private static final int NUM_COL_PRODUCER_NAME  = 2;
	
	private static final String COL_DENOMINATION = "denomination";
	
	private static final int NUM_COL_DENOMINATION  = 3;
	
	private static final String COL_VIN_TYPE = "vin_type";
	
	private static final int NUM_COL_VIN_TYPE  = 4;
	
	private static final String COL_CONTENT_ALCOHOL = "content_alcohol";
	
	private static final int NUM_COL_CONTENT_ALCOHOL  = 5;
	
	private static final String COL_VIN_ORIGIN= "vin_origin";
	
	private static final int NUM_COL_VIN_ORIGIN  = 6;
	
	private static final String COL_LATITUDE = "latitude";
	
	private static final int NUM_LATITUDE  = 7;
	
	private static final String COL_LONGITUDE = "longitude";
	
	private static final int NUM_LONGITUDE  = 8;
	
	private static final String COL_COUNTRY_ORIGIN = "country_origin";
	
	private static final int NUM_COUNTRY_ORIGIN  = 9;
	
	private static final String COL_CAPACITY = "capacity";
	
	private static final int NUM_CAPACITY  = 10;
	
	private static final String COL_COLOR = "color";
	
	private static final int NUM_COLOR  = 11;
	
	private static final String COL_VIN_SPECIFICATION = "vin_specification";
	
	private static final int NUM_VIN_SPECIFICATION  = 12;
	
	private static final String COL_GRAPE_HARVEST_YEARS = "grape_harvest_years";
	
	private static final int NUM_GRAPE_HARVEST_YEARS  = 13;
	
	private static final String COL_MATURATION_PERIOD = "maturation_period";
	
	private static final int NUM_MATURATION_PERIOD  = 14;

	public BottleBDD(Context context) {
		super(context);
	}
	
	public static long insertBottle(Bottle bottle ){
		//Création d'un ContentValues (fonctionne comme une HashMap)
		ContentValues values = new ContentValues();
		//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
		values.put(COL_DESIGNATION, bottle.getDesignation());
		values.put(COL_PRODUCERNAME, bottle.getProducerName());
		values.put(COL_DENOMINATION, bottle.getDenomination());
		values.put(COL_VIN_TYPE, bottle.getVinType());
		values.put(COL_CONTENT_ALCOHOL, bottle.getContentAlcohol());
		values.put(COL_VIN_ORIGIN, bottle.getVinOrigin());
		values.put(COL_LATITUDE, bottle.getLatitude());
		values.put(COL_LONGITUDE, bottle.getLongitude());
		values.put(COL_COUNTRY_ORIGIN, bottle.getCountryOrigin());
		values.put(COL_CAPACITY, bottle.getCapacity());
		values.put(COL_COLOR, bottle.getColor());
		values.put(COL_VIN_SPECIFICATION, bottle.getVinSpecification());
		values.put(COL_GRAPE_HARVEST_YEARS, bottle.getGrapeHarvestYears());
		values.put(COL_MATURATION_PERIOD, bottle.getMaturationPeriod());
		//on insère l'objet dans la BDD via le ContentValues
		return bdd.insert(TABLE_BOTTLE, null, values);
	}
	
	public static ArrayList<Bottle> getAllBottles () {
		Cursor c = bdd.query(TABLE_BOTTLE ,new String[] {COL_ID_BOTTLE, COL_DESIGNATION, COL_PRODUCERNAME, COL_DENOMINATION, COL_VIN_TYPE,COL_CONTENT_ALCOHOL, COL_VIN_ORIGIN, COL_LATITUDE, COL_LONGITUDE, COL_COUNTRY_ORIGIN,COL_CAPACITY, COL_COLOR,COL_VIN_SPECIFICATION, COL_GRAPE_HARVEST_YEARS,COL_MATURATION_PERIOD }
					,"", null, null, null, null);
		if (c == null || c.getCount() == 0 ) {
			return null;
		}
		return cursorToBottle(c);
	}

	private static ArrayList<Bottle> cursorToBottle(Cursor c) {
		ArrayList<Bottle> list = new ArrayList<Bottle>();
		Bottle bottle = null;
		if ( c == null || c.getCount() == 0)
			return list;
		for (int i=0;i< c.getCount(); i++) {
			c.moveToPosition(i);
			//On créé un user
			bottle = new Bottle();
			//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
			bottle.setId(c.getInt(NUM_COL_ID));
			bottle.setDesignation(c.getString(NUM_COL_DESIGNATION));
			bottle.setProducerName(c.getString(NUM_COL_PRODUCER_NAME));
			bottle.setDenomination(c.getString(NUM_COL_DENOMINATION));
			bottle.setVinType(c.getString(NUM_COL_VIN_TYPE));
			bottle.setContentAlcohol(c.getString(NUM_COL_CONTENT_ALCOHOL));
			bottle.setVinOrigin(c.getString(NUM_COL_VIN_ORIGIN));
			bottle.setLatitude(c.getString(NUM_LATITUDE));
			bottle.setLongitude(c.getString(NUM_LONGITUDE));
			bottle.setCountryOrigin(c.getString(NUM_COUNTRY_ORIGIN));
			bottle.setCapacity(c.getString(NUM_CAPACITY));
			bottle.setColor(c.getString(NUM_COLOR));
			bottle.setVinSpecification(c.getString(NUM_VIN_SPECIFICATION));
			bottle.setGrapeHarvestYears(c.getString(NUM_GRAPE_HARVEST_YEARS));
			bottle.setMaturationPeriod(c.getString(NUM_MATURATION_PERIOD));
			list.add(bottle);
		}
		//On ferme le cursor
		c.close();
 
		//On retourne le user
		return list;
	}

}
